<?php

namespace App\Http\Controllers\WorkOrder\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\UserPermissionAuth;
use App\Models\WorkOrder\ProductSelectionProposal\WorkOrderProductSelectionProposalModel;
use App\Service\WorkOrder\ProductSelectionProposal\ProductSelectionProposalService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ProductSelectionProposalController extends BaseController
{

    /**
     * 获取表单
     * @param Request $request
     * @return JsonResponse
     */
    public function getForm(Request $request): JsonResponse
    {
        $userId     = getRmsUserId();
        $userName   = getRmsUserInfoByAuth()->username ?? "";
        $userMobile = getRmsUserInfoByAuth()->mobile ?? "";
        $form       = ProductSelectionProposalService::getProductSelectionProposalForm($userId, $userMobile, $userName);
        return $this->success($form);
    }

    /**
     * 获取提交次数
     * @param Request $request
     * @return JsonResponse
     */
    public function getSubmitNum(Request $request): JsonResponse
    {
        $userId = getRmsUserId();
        $form   = ProductSelectionProposalService::getSubmitNum($userId);
        return $this->success($form);
    }


    /**
     * 提交表单
     * @param Request $request
     * @return JsonResponse
     */
    public function submitForm(Request $request): JsonResponse
    {
        $shopId             = $request->header('shopId', 0);
        $headerPlatformName = $request->header('platformName', '');// 选品建议飞书端

        $userId   = getRmsUserId();
        $userName = getRmsUserInfoByAuth()->username ?? '';

        $type = WorkOrderProductSelectionProposalModel::TYPE_FRANCHISEE;// 加盟商
        if ($headerPlatformName == UserPermissionAuth::MODULE_PRODUCT_SELECTION_PROPOSAL) {// 选品建议飞书端请求
            $type = WorkOrderProductSelectionProposalModel::TYPE_STAFF;                    // 员工
        }
        ProductSelectionProposalService::submitForm($request->all(), $shopId, $userId, $userName, $type);
        return $this->success();
    }


    /**
     * 获取历史提交记录
     * @param Request $request
     * @return JsonResponse
     */
    public function lists(Request $request): JsonResponse
    {
        $pageNum  = $request->get('pageNum', 1);
        $pageSize = $request->get('pageSize', 15);

        $userId   = getRmsUserId();

        return $this->success(ProductSelectionProposalService::lists($userId, $pageNum, $pageSize));
    }


    /**
     * 通过工单号获取详情
     * @param Request $request
     * @param string $workOrderNo
     * @return JsonResponse
     */
    public function detail(Request $request, string $workOrderNo): JsonResponse
    {
        $userId = getRmsUserId();

        return $this->success(ProductSelectionProposalService::detail($workOrderNo, $userId));
    }
}
